﻿<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>
$(function(){
	//弹出框设置
	$("#dialogDiv").dialog({
		title:"文档信息",//标题
		width: 800,//宽度
	    height: 350,//高度
	    closed: true,//初始关闭
	    cache: false,//缓存
	    modal: true,
	    onClose: function() {
			 $("#dialogIframe").attr("src","");
	    }
	});
	
	
	//新增按钮绑定点击事件
	$("#addButton").click(function(){
		var catalogId = $("#catalogId").val();
		$("#dialogIframe").attr("src","<c:url value='/docInfo/docInfoDataAdd?catalogId="+catalogId+"' />");
		$('#dialogDiv').dialog('open').window('center');
	});
	
	//编辑按钮绑定点击事件
	$("#updateButton").click(function(){
		if($("input[name='id']:checked").size()==1){
			$("#dialogIframe").attr("src","<c:url value='/docInfo/docInfoDataEdit?'/>id="+$("input[name='id']:checked").val());
			$('#dialogDiv').dialog('open').window('center');
		}else{
			$.messager.alert('提示','请选择一条数据再修改');
		}
	});
	
	//查看按钮绑定点击事件
	$("#showButton").click(function(){
			if($("input[name='id']:checked").size()==1){
				$("#dialogIframe").attr("src","<c:url value='/docInfo/docInfoDataShow?'/>id="+$("input[name='id']:checked").val());
				$('#dialogDiv').dialog('open').window('center');
			}else{
				$.messager.alert('提示','请选择一条数据再查看');
			}
	});
	
	//更新版本按钮绑定点击事件
	$("#bbgxButton").click(function(){
			if($("input[name='id']:checked").size()==1){
				$("#dialogIframe").attr("src","<c:url value='/docInfo/docInfoDataBbUpdate?'/>id="+$("input[name='id']:checked").val());
				$('#dialogDiv').dialog('open').window('center');
			}else{
				$.messager.alert('提示','请选择一条数据再更新');
			}
	});
	
	//删除按钮绑定点击事件
	$("#deleteButton").click(function(){
		var checkValues = getCheckBoxValue("id");
		//判断是否选择了数据行
		if(null == checkValues || '' == checkValues){
			$.messager.alert('提示','请先选择要删除的行！');
			return;
		}
		$.messager.confirm('提示', '确定要删除数据吗?', function(flag){
			if(flag){
				$.ajax({
					type: "POST",
					url: "<c:url value='/docInfo/deleteDocData' />",
					data: "ids="+checkValues,
					datatype: "json",
					success: function(msg){
						    var flag=false;
			                try {
								flag=JSON.parse(msg).state=="200";
						    } catch (e) {
								flag=JSON.stringify(msg).indexOf("200")!=-1;
							}
						    if(flag){
						    	$.messager.alert('提示','数据删除成功');
								$('#docInfoTable').datagrid({ queryParams:getParams()});
						    }else{
						    	$.messager.alert('提示',"数据删除失败");
						    }
					}
				});
			}
		});
		
	});
	
	//表格加载
	var _grid=$('#docInfoTable').datagrid({
		    url:'<c:url value="/docInfo/affDocumentData" />',
		    queryParams:getParams(),//提交参数
		    toolbar:'#toolbar',//表头工具栏
		    pagination:true,//显示分页  
		    pagePosition:'bottom',//分页栏出现位置
		    pageNumber:1,//初始页码
		    pageSize:10,//初始每页显示记录数量
		    pageList: [10,20,30,40,50],
		    emptyMsg:'数据为空',
		    nowrap:false,//列的内容超出所定义的列宽时,自动换行
		    fitColumns:true,//设置为 true，则会自动扩大或缩小列的尺寸以适应网格的宽度并且防止水平滚动。
		    striped:true,//设置为 true，则把行条纹化。（即奇偶行使用不同背景色）
		    //rownumbers:true,//设置为 true，则显示带有行号的列。
		    //singleSelect:true,
		    columns:[[
		        {field:'id',title:'',resizable:false,align:'center',halign:'center',checkbox:true,width:"5%"},
		        {field:'index',title:'序号',resizable:false,align:'center',halign:'center',width:"5%",formatter:function(val,row,index){
		        	var options = $("#docInfoTable").datagrid('options'); 
		        	var currentPage = options.pageNumber;
		        	var pageSize = options.pageSize;
		        	return (pageSize * (currentPage -1))+(index+1);
		        }},
				{field:'title',title:'标题',resizable:false,width:"20%",formatter: function(value,row,index){
					return "<a class='textClick' href='###' onclick=\"showInfo('"+row.id+"')\" >"+value+"</a>";
				}},
				{field:'docSize',title:'大小',resizable:false,width:"8%" },
				{field:'docVcreater',title:'上传人',resizable:false,width:"11%" },
				{field:'docVcreateTime',title:'上传日期',width:"20%",formatter: function(value,row,index){
					var fromDate="";
					if(value){
						fromDate=dataFormatStr(new Date(value),"yyyy-MM-dd hh:mm:ss");
					}
					return fromDate;
				}},
				{field:'catalogId',title:'操作',resizable:false,width:"14%",formatter: function(value,row,index){
					var html = "<a class='textClick' href='<c:url value='/docInfo/downLoadFile' />?docId="+row.id+"'>下载</a>";
					html += "&nbsp;&nbsp;<a class='textClick' href='###' onclick=\"showRecordVersion('"+row.id+"')\" >历史版本</a>";
					return html;
				}}
		    ]]
		});
		
});

function showInfo(id){
	$("#dialogIframe").attr("src","<c:url value='/docInfo/docInfoDataShow?'/>id="+id);
	$('#dialogDiv').dialog('open').window('center');
}

/**
 * 历史版本查看
 */
function showRecordVersion(id){
	$("#dialogIframe").attr("src","<c:url value='/docInfo/docInfoDocVersionList?'/>id="+id);
	$('#dialogDiv').dialog('open').window('center');
}

//根据checkbox的名称获取选中的值(返回：a,b,c)
function getCheckBoxValue(coulmnName){   
	var arr=document.getElementsByName(coulmnName);	
  var check_val="";
	for(i=0;i<arr.length;i++){
		if(arr[i].checked){
			if(check_val==""){
				check_val+=arr[i].value;
			}else{
				check_val+=",";
				check_val+=arr[i].value;				
			}						
		}
	}
	return check_val;
}

//查询条件生成方法
function getParams(){
	var jsonObj=new Object();
	jsonObj.catalogId="<c:out value='${catalogId}' />";
	return jsonObj;
}
//关闭弹出框
function closeDialog(){
	$('#docInfoTable').datagrid({ queryParams:getParams()});
	$('#dialogDiv').dialog('close');
}
function closeDialogDiv(){
	$('#dialogDiv').dialog('close');
}

function parentGridReload(){
	$('#docInfoTable').datagrid({ queryParams:getParams()});
}

